package com.miaoshapro.service.impl;

import com.miaoshapro.dao.UserDOMapper;
import com.miaoshapro.dao.UserPasswordDOMapper;
import com.miaoshapro.dataobject.UserDO;
import com.miaoshapro.dataobject.UserPasswordDO;
import com.miaoshapro.error.ErrorEnum;
import com.miaoshapro.exception.CommonException;
import com.miaoshapro.service.UserService;
import com.miaoshapro.service.model.UserModel;
import com.miaoshapro.validator.ValidationResult;
import com.miaoshapro.validator.ValidatorImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private UserPasswordDOMapper userPasswordDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel getUserById(Integer id) {
        UserDO userDO = userDOMapper.selectByPrimaryKey(id);
        if (userDO == null)
            return null;
        UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
        return convertFromDataObject(userDO, userPasswordDO);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveUserInfo(UserModel userModel) throws CommonException {

        ValidationResult result = validator.validate(userModel);
        if (result.getHasErrors()) {
            throw new CommonException(ErrorEnum.PARAMETER_ERROR, result.getErrorMsg());
        }

        UserDO userDO = convertUserDOFromModel(userModel);
        try {
            userDOMapper.insertSelective(userDO);
        } catch (DuplicateKeyException e) {
            throw new CommonException(ErrorEnum.INVALID_PARAMETER, "用户手机号码已经存在");
        }

        userModel.setId(userDO.getId());
        UserPasswordDO userPasswordDO = convertUserPasswordDOFromModel(userModel);
        userPasswordDOMapper.insertSelective(userPasswordDO);

    }

    private UserDO convertUserDOFromModel(UserModel userModel) {
        if (userModel == null)
            return null;
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userModel, userDO);
        return userDO;
    }

    private UserPasswordDO convertUserPasswordDOFromModel(UserModel userModel) {
        if (userModel == null)
            return null;
        UserPasswordDO userPasswordDO = new UserPasswordDO();
        userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDO.setUserId(userModel.getId());
        return userPasswordDO;
    }


    private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
        if (userDO == null) {
            return null;
        }

        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO, userModel);
        userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
        return userModel;
    }
}
